package com.hjm.project.terminal.detail.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.hjm.common.utils.security.ShiroUtils;
import com.hjm.project.profitDay.detailed.domain.ProfitDayDetailed;
import com.hjm.project.profitDay.detailed.domain.ProfitDayDetailedReturn;
import com.hjm.project.profitDay.detailed.service.IProfitDayDetailedService;
import com.hjm.project.terminal.detail.domain.TerminalRecordDetail;
import com.hjm.project.terminal.detail.domain.TerminalRecordDetailVo;
import com.hjm.project.terminal.detail.service.ITerminalRecordDetailService;
import com.hjm.project.terminal.manufacturer.service.IManufacturerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.common.utils.poi.ExcelUtil;
import com.hjm.framework.web.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 终端调拨详情Controller
 * 
 * @author LOVE
 * @date 2020-11-28
 */
@Controller
@RequestMapping("/system/detail")
public class TerminalRecordDetailController extends BaseController
{
    private String prefix = "system/detail";

    @Autowired
    private ITerminalRecordDetailService terminalRecordDetailService;
    @Autowired
    IProfitDayDetailedService profitDayDetailedService;
    @Resource
    private IManufacturerService manufacturerService;


    @RequiresPermissions("system:detail:view")
    @GetMapping()
    public String detail()
    {
        return prefix + "/detail";
    }

    /**
     * 查询终端调拨详情列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map map, TerminalRecordDetail terminalRecordDetail)
    {
        startPage();
        List<TerminalRecordDetail> list = terminalRecordDetailService.selectTerminalRecordDetailList(terminalRecordDetail);
        return getDataTable(list);
    }

    /**
     * 导出终端调拨详情列表
     */
    @RequiresPermissions("system:detail:export")
    @Log(title = "终端调拨详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TerminalRecordDetail terminalRecordDetail)
    {
        List<TerminalRecordDetail> list = terminalRecordDetailService.selectTerminalRecordDetailList(terminalRecordDetail);
        ExcelUtil<TerminalRecordDetail> util = new ExcelUtil<TerminalRecordDetail>(TerminalRecordDetail.class);
        return util.exportExcel(list, "detail");
    }

    /**
     * 新增终端调拨详情
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存终端调拨详情
     */
    @RequiresPermissions("system:detail:add")
    @Log(title = "终端调拨详情", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TerminalRecordDetail terminalRecordDetail)
    {
        return toAjax(terminalRecordDetailService.insertTerminalRecordDetail(terminalRecordDetail));
    }

    /**
     * 修改终端调拨详情
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TerminalRecordDetail terminalRecordDetail = terminalRecordDetailService.selectTerminalRecordDetailById(id);
        mmap.put("terminalRecordDetail", terminalRecordDetail);
        return prefix + "/edit";
    }

    /**
     * 修改保存终端调拨详情
     */
    @RequiresPermissions("system:detail:edit")
    @Log(title = "终端调拨详情", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TerminalRecordDetail terminalRecordDetail)
    {
        return toAjax(terminalRecordDetailService.updateTerminalRecordDetail(terminalRecordDetail));
    }

    /**
     * 删除终端调拨详情
     */
    @RequiresPermissions("system:detail:remove")
    @Log(title = "终端调拨详情", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(terminalRecordDetailService.deleteTerminalRecordDetailByIds(ids));
    }
    /**
     * 查询卡费返现列表
     * @return
     */
    @GetMapping("/cardFee")
    public String cardFee(ModelMap mmap) throws Exception {
        List<Map> manufacturer = manufacturerService.getManufacturer(ShiroUtils.isAgent() ? ShiroUtils.getLoginAgentNum() : null);
        mmap.put("manufacturer",manufacturer);
        return "terminal/detail/cardFee";
    }

    /**
     * 查询卡费返现列表
     */
    @PostMapping("/cardFeeList")
    @ResponseBody
    public TableDataInfo cardFeeList(ProfitDayDetailed profitDayDetailed){
        if (ShiroUtils.isAgent()) {
            profitDayDetailed.setLoginAgentNum(ShiroUtils.getLoginAgentNum());
        }
        Map pd = profitDayDetailedService.selectCardFeeSum(profitDayDetailed);
        startPage();
        List<ProfitDayDetailed> list = profitDayDetailedService.selectCardFeeList(profitDayDetailed);
        for(ProfitDayDetailed pdd :list){
            if(pdd.getTaxPoint() != null && !pdd.getTaxPoint().equals("0"))
            pdd.setTaxPoint(String.valueOf(Double.parseDouble(pdd.getTaxPoint())*100));
        }
        return getDataTable(list,pd);
    }
    /**
     * 导出终端调拨详情列表
     */
    @RequiresPermissions("system:detail:export")
    @Log(title = "终端调拨详情", businessType = BusinessType.EXPORT)
    @PostMapping("/exportCard")
    @ResponseBody
    public AjaxResult exportCard(ProfitDayDetailed profitDayDetailed)
    {
        List<TerminalRecordDetailVo> list = profitDayDetailedService.selectCardFeeListVo(profitDayDetailed);
        ExcelUtil<TerminalRecordDetailVo> util = new ExcelUtil(TerminalRecordDetailVo.class);
        return util.exportExcel(list, "cardFee");
    }
    @Log(title = "终端返现明细", businessType = BusinessType.EXPORT)
    @PostMapping("/exportCardReturn")
    @ResponseBody
    public AjaxResult exportCardReturn(ProfitDayDetailed profitDayDetailed)
    {
        if (ShiroUtils.isAgent()) {
            profitDayDetailed.setLoginAgentNum(ShiroUtils.getLoginAgentNum());
        }
        List<ProfitDayDetailed> list = profitDayDetailedService.selectCardFeeList(profitDayDetailed);
        List<ProfitDayDetailedReturn> lists = new ArrayList<>();
        for(ProfitDayDetailed pdd :list){
            ProfitDayDetailedReturn pd = new ProfitDayDetailedReturn();
            if(pdd.getTaxPoint() != null && !pdd.getTaxPoint().equals("0")) {
                pdd.setTaxPoint(String.valueOf(Double.parseDouble(pdd.getTaxPoint()) * 100));
            }
            BeanUtils.copyProperties(pdd,pd);
            lists.add(pd);
        }
        ExcelUtil<ProfitDayDetailedReturn> util = new ExcelUtil(ProfitDayDetailedReturn.class);
        return util.exportExcel(lists, "终端返现明细");
    }
}
